<template>
	<view class="container">
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400">
				<swiper-item class="swiper-item" v-for="(item,index) in detailData.imgList" :key="index">
					<view class="image-wrapper">
						<image :src="item" class="loaded" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<view class="introduce-section">
			<text class="title" style="font-weight: bold;">{{detailData.title}}</text>

			<!-- <view style="color: #606266;">{{detailData.resourceIntro}}</view> -->
			<view class="price-box">
				<text class="price-tip">¥</text>
				<text class="price" style="font-weight: bold;">{{detailData.price}}</text>
				<text class="m-price">¥{{detailData.originPrice}}积分</text>

			</view>
			<view style="display: flex;flex-direction: row;align-items: center;
			justify-content: space-between;color: #909399;font-size: 28upx;">
			<view style="background-color: #F6CF66;color: #181818;height: 40upx;
			line-height: 34upx;border-radius: 20upx;font-size: 20upx;padding: 4upx;padding-left: 20upx;
			padding-right: 20upx;">{{detailData.resourceTypeName}}</view>
			
								<text>销量: {{detailData.buyCount}}</text>
				<text >浏览量: {{detailData.viewCount}}</text>
				
			</view>
		</view>

		<!--  分享 -->




		<view class="detail-desc">
			<view class="d-header">
				<text style="font-weight: bold;">资源详情</text>
			</view>
			<view style="margin-left: 20upx;margin-right: 20upx;color: #444444;">
				<rich-text :nodes="desc" @touchstart="catchImage"></rich-text>
				<!-- <text>{{detailData.description}}</text> -->
			</view>

		</view>

		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/pages/cart/cart" open-type="switchTab" class="p-b-btn">
				<view class="" style="display: flex;flex-direction: column;align-items: center;">
					<button style="display: flex;flex-direction: column;justify-content: center;
						background-color:transparent !important;border: none !important;outline: none !important;color: #fff;"
						open-type="contact" bindcontact="handleContact" session-from="sessionFrom">
						<image style="width: 48upx;height: 48upx;" src="../../static/icon/kefu2.png" mode=""></image>
					</button>
					<text>客服</text>
				</view>
			</navigator>
			<navigator url="/pages/cart/cart" open-type="switchTab" class="p-b-btn">
				<view class="" style="display: flex;flex-direction: column;align-items: center;">
					<button style="display: flex;flex-direction: column;justify-content: center;
						background-color:transparent !important;border: none !important;outline: none !important;color: #fff;"
						@click="shareDemand" open-type="share">
						<image style="width: 50upx;height: 50upx;" src="../../static/icon/share-icon333.png" mode="">
						</image>
					</button>
					<text>分享</text>
				</view>

			</navigator>


			<view class="p-b-btn" @click="toFavorite(2)" v-if="detailData.isCollect">
				<image src="../../static/images/collected.png" style="width: 52upx;height: 52upx;"></image>
				<text>已收藏</text>
			</view>


			<view class="p-b-btn" @click="toFavorite(1)" v-else>
				<image src="../../static/images/no-collect.png" style="width: 52upx;height: 52upx;"></image>
				<text>收藏</text>
			</view>


			<view class="action-btn-group">
			<!-- 	<button type="primary" class="action-btn no-border buy-now-btn" @click="doPay"
					v-if="detailData.price>0">立即购买</button> -->
					
					<button type="primary" class="action-btn no-border buy-now-btn" @click="pointChange"
						v-if="detailData.price>0">积分兑换</button>
				<button type="primary" class="action-btn no-border add-cart-btn" @click="freeGet(detailData.id)"
					v-else>免费获取</button>
			</view>
		</view>
		
		
		<view class="popup" v-show="show">
					<view class="popup-info">
						<view style="color: #303133;text-align: center;font-size: 26upx;font-weight: bold;">联系平台</view>
						<image src="../../static/images/mine-wechat.jpg" mode="" style="width: 500upx;height: 500upx;margin-top: 20upx;"></image>
						<view style="color: #191919;font-size: 28upx;margin-top: 48upx;width: 500upx;word-wrap: break-word; 
		word-break: normal;background-color: #f3f3f3;border-radius: 20upx;padding: 20upx 20upx">作者微信:yuanmazijie-2022</view>
		
						<view
							style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;margin-top: 56upx;">
							<view style="background: #F4F4F4;color: #191919;font-size: 28upx;width: 240upx;height: 80upx;margin-left: 30upx;
									border-radius: 32upx;text-align: center;line-height: 80upx;" @tap="cancel">取消</view>
							<view style="background: #4CD964;color: #FFFFFF;font-size: 28upx;width: 240upx;
									height: 80upx;margin-right: 30upx;border-radius: 32upx;text-align: center;line-height: 80upx;"
								@tap="copy('yuanmazijie-2022')">复制
							</view>
						</view>
					</view>
				</view>



		<!-- 分享 -->
		<!-- 	<share 
			ref="share" 
			:contentHeight="580"
			:shareList="shareList"
		></share> -->
	</view>
</template>

<script>
	// import share from '@/components/share';
	import {
		resource_code_detail
	} from '../../api/resource_code.js'
	import {
		do_pay,
		free_resource_code
	} from '../../api/trade.js'
	import {
		user_collect
	} from '../../api/user_collect.js'
	
	import {pay_config} from '../../api/config.js'

	export default {
		// components: {
		// 	share
		// },
		data() {
			return {
				specClass: 'none',
				detailId: '',
				specSelected: [],
				imgarr: [],

				favorite: true,
				shareList: [],
				payType:"",
				detailData: {},
				imgList: [

				],
				desc: `
					
				`,
				show:false,
				specList: [{
						id: 1,
						name: '尺寸',
					},
					{
						id: 2,
						name: '颜色',
					},
				],

			};
		},
		onLoad(options) {
			
			this.getPayConfig()
			//接收传值,id里面放的是标题，因为测试数据并没写id 
			let id = options.id;
			this.detailId = id
			console.log(id)
			this.getDetail(id)
		},
		methods: {
			
			catchImage(e){
				console.log(e)
			    wx.previewImage({
			      current: this.imgarr[0], // 当前显示图片的http链接
			      urls: this.imgarr // 需要预览的图片http链接列表
			    })
			  },

			pointChange(){
				if(this.payType==='0'){
					this.exchange()
				}else{
					this.doPay()
				}
				
			},
			
			getPayConfig(){
				pay_config({}).then(res=>{
					console.log(res)
					this.payType=res.data
				})
			},
			
			cancel(){
				this.show=false
			},
			exchange(){
				this.show=true
				
			},
			// 点击弹窗确认
			copy(value) {
				uni.setClipboardData({
					data: value, //要被复制的内容
					success: () => { //复制成功的回调函数
						uni.showToast({ //提示
							title: '复制成功',
							icon:'none'
						})
					}
				});
			
			},
			handleContact(e) {
				console.log(e.detail.path)
				console.log(e.detail.query)
			},
			doPay() {
				let params = {}
				params.resourceId = this.detailId
				do_pay(params).then(res => {
					console.log(res)
					if (res.data !== null) {
						console.log(res.data)
						// 仅作为示例，非真实参数信息。
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data._package,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(res) {
								// console.log('success:' + JSON.stringify(res));
								
								uni.navigateTo({
									url:'../mine-resource/mine-resource'
								})
								
							},
							fail: function(err) {
								console.log('fail:' + JSON.stringify(err));
							}
						});
					}
				})
			},
			getDetail(id) {

				if (id) {
					let params = {}
					params.id = id
					resource_code_detail(params).then(res => {
						console.log(res)
						if (res.status === 3001) {
							setTimeout(function() {
								uni.navigateTo({
									url: '../login/login'
								})
							
							}, 2000)
							
						} else {
							this.detailData = res.data
							this.desc = this.formatRichText(res.data.description)
							let regex = new RegExp(/<img.*?(?:>|\/>)/gi); // 匹配所有图片
							let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; // 匹配src图片
							let arrsImg = res.data.description.match(regex);
							
							let imgarr = [];
							for (let a = 0; a < arrsImg.length; a++) {
							    let srcs = arrsImg[a].match(srcReg);
							    imgarr.push(srcs[1])
							}
							this.imgarr=imgarr
						}

					})
				}

			},

			freeGet(id) {
				// uni.showToast({
				// 	title: '这是免费获取看视频',
				// 	icon: 'none'
				// })
				let params = {}
				params.resourceCodeId = id

				free_resource_code(params).then(res => {
					if(res.status !==7001){
						uni.showToast({
							title: '获取成功',
							icon: 'none',
							duration: 2000
						})
						setTimeout(function() {
							uni.navigateTo({
								url: '../mine-resource/mine-resource'
							})
						
						}, 2000)
					}
					

				})

			},
			//规格弹窗开关
			toggleSpec() {
				if (this.specClass === 'show') {
					this.specClass = 'hide';
					setTimeout(() => {
						this.specClass = 'none';
					}, 250);
				} else if (this.specClass === 'none') {
					this.specClass = 'show';
				}
			},
			appShare(val) {
				uni.share({
					provider: "weixin",
					scene: "WXSceneSession",
					type: 0,
					title: this.detailData.title,
					summary: this.detailData.resourceIntro,
					imageUrl: this.detailData.imgList[0],
					success: function(res) {
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
			},
			//选择规格
			selectSpec(index, pid) {
				let list = this.specChildList;
				list.forEach(item => {
					if (item.pid === pid) {
						this.$set(item, 'selected', false);
					}
				})

				this.$set(list[index], 'selected', true);
				//存储已选择
				/**
				 * 修复选择规格存储错误
				 * 将这几行代码替换即可
				 * 选择的规格存放在specSelected中
				 */
				this.specSelected = [];
				list.forEach(item => {
					if (item.selected === true) {
						this.specSelected.push(item);
					}
				})

			},
			//分享
			share() {
				this.$refs.share.toggleMask();
			},
			//收藏
			toFavorite(type) {
				if (type == 1) {
					
					
					
					let params = {}
					params.type = 1
					params.resourceCodeId = this.detailId
					user_collect(params).then(res => {
						if(res.status === 3001){
							uni.navigateTo({
								url:'../login/login'
							})
							return
						}else{
							setTimeout(()=>{
								uni.showToast({
									title: '收藏成功',
									icon: 'none',
									duration: 2000
								})
							}, 200)
							this.getDetail(this.detailId)
						}
						
						
					})
						
					
						
						
			
				} else {	
						
					let params = {}
					params.type = 2
					params.resourceCodeId = this.detailId
					user_collect(params).then(res => {}).then(() => {
						
						this.getDetail(this.detailId)
						
						setTimeout(()=>{
							uni.showToast({
								title: '取消成功',
								icon: 'none',
								duration: 5000
							})
						}, 200)
						
					})
				}

			},
			buy() {
				uni.navigateTo({
					url: `/pages/order/createOrder`
				})
			},
			//解析富文本方法
			formatRichText(html) {
				let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
					match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
					match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
					match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
					return match;
				});
				newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
					match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
						'max-width:100%;');
					return match;
				});
				newContent = newContent.replace(/<br[^>]*\/>/gi, '');
				newContent = newContent.replace(/\<img/gi,
					'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
				return newContent;
			},
			stopPrevent() {}
		},

	}
</script>

<style lang='scss'>
	page {
		background: $page-color-base;
		padding-bottom: 160rpx;
	}

	.icon-you {
		font-size: $font-base + 2rpx;
		color: #888;
	}

	.carousel {
		height: 722rpx;
		position: relative;

		swiper {
			height: 100%;
		}

		.image-wrapper {
			width: 100%;
			/* height: 100%; */
		}

		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

	}

	/* 标题简介 */
	.introduce-section {
		background: #fff;
		padding: 20rpx 30rpx;

		.title {
			font-size: 32rpx;
			color: $font-color-dark;
			height: 50rpx;
			line-height: 50rpx;
		}

		.price-box {
			display: flex;
			align-items: baseline;
			height: 64rpx;
			padding: 10rpx 0;
			font-size: 26rpx;
			color: $uni-color-primary;
		}

		.price {
			font-size: $font-lg + 2rpx;
		}

		.m-price {
			margin: 0 12rpx;
			color: $font-color-light;
			text-decoration: line-through;
		}

		.coupon-tip {
			align-items: center;
			padding: 4rpx 10rpx;
			background: $uni-color-primary;
			font-size: $font-sm;
			color: #fff;
			border-radius: 6rpx;
			line-height: 1;
			transform: translateY(-4rpx);
		}

		/* .bot-row {
			display: flex;
			align-items: center;
			height: 50rpx;
			font-size: $font-sm;
			color: $font-color-light;

			text {
				flex: 1;
			}
		} */
	}

	/* 分享 */
	.share-section {
		display: flex;
		align-items: center;
		color: $font-color-base;
		background: linear-gradient(left, #fdf5f6, #fbebf6);
		padding: 12rpx 30rpx;

		.share-icon {
			display: flex;
			align-items: center;
			width: 70rpx;
			height: 30rpx;
			line-height: 1;
			border: 1px solid $uni-color-primary;
			border-radius: 4rpx;
			position: relative;
			overflow: hidden;
			font-size: 22rpx;
			color: $uni-color-primary;

			&:after {
				content: '';
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				left: -20rpx;
				top: -12rpx;
				position: absolute;
				background: $uni-color-primary;
			}
		}

		.icon-xingxing {
			position: relative;
			z-index: 1;
			font-size: 24rpx;
			margin-left: 2rpx;
			margin-right: 10rpx;
			color: #fff;
			line-height: 1;
		}

		.tit {
			font-size: $font-base;
			margin-left: 10rpx;
		}

		.icon-bangzhu1 {
			padding: 10rpx;
			font-size: 30rpx;
			line-height: 1;
		}

		.share-btn {
			flex: 1;
			text-align: right;
			font-size: $font-sm;
			color: $uni-color-primary;
		}

		.icon-you {
			font-size: $font-sm;
			margin-left: 4rpx;
			color: $uni-color-primary;
		}
	}

	.c-list {
		font-size: $font-sm + 2rpx;
		color: $font-color-base;
		background: #fff;

		.c-row {
			display: flex;
			align-items: center;
			padding: 20rpx 30rpx;
			position: relative;
		}

		.tit {
			width: 140rpx;
		}

		.con {
			flex: 1;
			color: $font-color-dark;

			.selected-text {
				margin-right: 10rpx;
			}
		}

		.bz-list {
			height: 40rpx;
			font-size: $font-sm+2rpx;
			color: $font-color-dark;

			text {
				display: inline-block;
				margin-right: 30rpx;
			}
		}

		.con-list {
			flex: 1;
			display: flex;
			flex-direction: column;
			color: $font-color-dark;
			line-height: 40rpx;
		}

		.red {
			color: $uni-color-primary;
		}
	}

	/* 评价 */
	.eva-section {
		display: flex;
		flex-direction: column;
		padding: 20rpx 30rpx;
		background: #fff;
		margin-top: 16rpx;

		.e-header {
			display: flex;
			align-items: center;
			height: 70rpx;
			font-size: $font-sm + 2rpx;
			color: $font-color-light;

			.tit {
				font-size: $font-base + 2rpx;
				color: $font-color-dark;
				margin-right: 4rpx;
			}

			.tip {
				flex: 1;
				text-align: right;
			}

			.icon-you {
				margin-left: 10rpx;
			}
		}
	}

	.eva-box {
		display: flex;
		padding: 20rpx 0;

		.portrait {
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 100px;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-base;
			color: $font-color-base;
			padding-left: 26rpx;

			.con {
				font-size: $font-base;
				color: $font-color-dark;
				padding: 20rpx 0;
			}

			.bot {
				display: flex;
				justify-content: space-between;
				font-size: $font-sm;
				color: $font-color-light;
			}
		}
	}

	/*  详情 */
	.detail-desc {
		background: #fff;
		margin-top: 16rpx;

		.d-header {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			font-size: $font-base + 2rpx;
			color: $font-color-dark;
			position: relative;

			text {
				padding: 0 20rpx;
				background: #fff;
				position: relative;
				z-index: 1;
			}

			&:after {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%);
				width: 300rpx;
				height: 0;
				content: '';
				border-bottom: 1px solid #ccc;
			}
		}
	}

	/* 规格选择弹窗 */
	.attr-content {
		padding: 10rpx 30rpx;

		.a-t {
			display: flex;

			image {
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -40rpx;
				border-radius: 8rpx;
				;
			}

			.right {
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				font-size: $font-sm + 2rpx;
				color: $font-color-base;
				line-height: 42rpx;

				.price {
					font-size: $font-lg;
					color: $uni-color-primary;
					margin-bottom: 10rpx;
				}

				.selected-text {
					margin-right: 10rpx;
				}
			}
		}

		.attr-list {
			display: flex;
			flex-direction: column;
			font-size: $font-base + 2rpx;
			color: $font-color-base;
			padding-top: 30rpx;
			padding-left: 10rpx;
		}

		.item-list {
			padding: 20rpx 0 0;
			display: flex;
			flex-wrap: wrap;

			text {
				display: flex;
				align-items: center;
				justify-content: center;
				background: #eee;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				border-radius: 100rpx;
				min-width: 60rpx;
				height: 60rpx;
				padding: 0 20rpx;
				font-size: $font-base;
				color: $font-color-dark;
			}

			.selected {
				background: #fbebee;
				color: $uni-color-primary;
			}
		}
	}

	button::after {
		border: none;
	}
	.popup-info {
		position: fixed;
		width: 600upx;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		padding: 40upx;
		padding-left: 50upx;
		border-radius: 20upx;
		background-color: #fff;
		z-index: 9999;
	}
	
	.popup {
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		height: 100vh;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: 9998;
	}

	/*  弹出层 */
	.popup1 {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;

		&.show {
			display: block;

			.mask {
				animation: showPopup 0.2s linear both;
			}

			.layer {
				animation: showLayer 0.2s linear both;
			}
		}

		&.hide {
			.mask {
				animation: hidePopup 0.2s linear both;
			}

			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}

		&.none {
			display: none;
		}

		.mask {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}

		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 100%;
			min-height: 40vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;

			.btn {
				height: 66rpx;
				line-height: 66rpx;
				border-radius: 100rpx;
				background: $uni-color-primary;
				font-size: $font-base + 2rpx;
				color: #fff;
				margin: 30rpx auto 20rpx;
			}
		}

		@keyframes showPopup {
			0% {
				opacity: 0;
			}

			100% {
				opacity: 1;
			}
		}

		@keyframes hidePopup {
			0% {
				opacity: 1;
			}

			100% {
				opacity: 0;
			}
		}

		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}

			100% {
				transform: translateY(0%);
			}
		}

		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}

			100% {
				transform: translateY(120%);
			}
		}
	}

	/* 底部操作菜单 */
	.page-bottom {
		position: fixed;
		left: 30rpx;
		bottom: 30rpx;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690rpx;
		height: 100rpx;
		background: rgba(255, 255, 255, .9);
		box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, .5);
		border-radius: 16rpx;

		.p-b-btn {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: $font-sm;
			color: $font-color-base;
			width: 96rpx;
			height: 80rpx;

			.yticon {
				font-size: 40rpx;
				line-height: 48rpx;
				color: $font-color-light;
			}

			&.active,
			&.active .yticon {
				color: $uni-color-primary;
			}

			.icon-fenxiang2 {
				font-size: 42rpx;
				transform: translateY(-2rpx);
			}

			.icon-shoucang {
				font-size: 46rpx;
			}
		}

		.action-btn-group {
			display: flex;
			height: 76rpx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20rpx 40rpx -16rpx #fa436a;
			box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
			background: linear-gradient(to right, #ffac30, #fa436a, #F56C6C);
			margin-left: 20rpx;
			position: relative;

			&:after {
				content: '';
				position: absolute;
				top: 50%;
				right: 50%;
				transform: translateY(-50%);
				height: 28rpx;
				width: 0;
				/* border-right: 1px solid rgba(255, 255, 255, .5); */
			}

			.action-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 360upx;
				height: 100%;
				font-size: $font-base;
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}
</style>
